package com.haruhi.bot.controller;

import com.alibaba.fastjson.JSONObject;
import com.haruhi.bot.entity.bot.PixPicture;
import com.haruhi.bot.entity.dto.BaseResponse;
import com.haruhi.bot.entity.dto.PixPictureCondition;
import com.haruhi.bot.job.thread.ExecuteCMDThread;
import com.haruhi.bot.job.thread.PixUploadProxyThread;
import com.haruhi.bot.job.thread.PutPicFromHaruhiThread;
import com.haruhi.bot.service.BotManage;
import com.haruhi.bot.service.PixPictureService;
import com.haruhi.bot.utils.CMDUtil;
import com.haruhi.bot.utils.ParamUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
public class ZhenXunBotController {

    @Autowired
    private PixPictureService pixPictureService;
    @Autowired
    private PixUploadProxyThread pixUploadProxyThread;

    @Value("${bot.port}")
    private String port;

    @Autowired
    private BotManage botManage;


    @PostMapping("/addPic")
    public String addPicBatch(@RequestBody List<PixPicture> list){
        System.out.println(JSONObject.toJSONString(list));
        return "ok";
    }

    @GetMapping("/putPixExecute")
    public Object putExecute(){
        PutPicFromHaruhiThread.execute(pixPictureService);
        return "任务开始执行";
    }

    @GetMapping("/allPixPicture")
    public Object allPixPicture(){
        return pixPictureService.allPix();
    }

    @PostMapping("/pixPicture")
    public BaseResponse getPixPicture(@RequestBody PixPictureCondition condition){
        if(condition == null || condition.isEmpty()){
            return BaseResponse.fail("参数错误");
        }
        return BaseResponse.success(0,"ok",pixPictureService.queryPixPicture(condition));
    }

    @PostMapping("/pixUpload")
    public BaseResponse upload(@RequestParam("file") List<MultipartFile> files, @RequestParam("tag") String tag, HttpServletRequest request){
        log.info("收到pix上传请求,IP:{},tag:{}",request.getRemoteAddr(),tag);
        try {
            if(files == null || files.size() == 0 || ParamUtil.isEmpty(tag)){
                return BaseResponse.fail();
            }
            Map<String, InputStream> map = new HashMap<>();
            for (MultipartFile file : files) {
                map.put(file.getOriginalFilename(),file.getInputStream());
            }
            pixUploadProxyThread.asyncExecute(pixPictureService,map,tag);
            return BaseResponse.success();
        }catch (Exception e){
            e.printStackTrace();
            log.error("controller发生错误:{}",e.getMessage());
            return BaseResponse.fail(e.getMessage());
        }
    }

    @GetMapping("/start")
    public BaseResponse start(){
        log.info("收到唤醒真寻请求");
        String pid = CMDUtil.getPID(port);
        if(pid != null){
            log.info("真寻已经是启动状态");
            return BaseResponse.fail("bot已为启动状态");
        }
        if(CMDUtil.isLinux()){
            botManage.startBot();
            log.info("已开始执行启动线程");
        }else {
            return BaseResponse.fail("检查系统出现错误,联系凉宫春日");
        }
        return BaseResponse.success("正在启动中...请等待1-2分钟后@小真寻以检查是否启动成功");

    }


}
